package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxWhList;
import com.adk.backend.service.WhListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库列表管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/wh-list")
public class WhListController {
    
    @Autowired
    private WhListService whListService;
    
    /**
     * 分页查询仓库列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxWhList>> getWhList(
            @RequestParam(required = false) String whName,
            @RequestParam(required = false) String whCode,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Long parentId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("whName", whName);
        params.put("whCode", whCode);
        params.put("type", type);
        params.put("parentId", parentId);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxWhList> result = whListService.getWhList(params);
        return Result.success(result);
    }
    
    /**
     * 获取所有仓库（不分页）
     */
    @RequiresPermission("view")
    @GetMapping("/all")
    public Result<List<LxWhList>> getAllWhList(@RequestParam(required = false) Integer type) {
        List<LxWhList> result = whListService.getAllWhList(type);
        return Result.success(result);
    }
    
    /**
     * 根据父级ID获取子仓库列表
     */
    @RequiresPermission("view")
    @GetMapping("/by-parent")
    public Result<List<LxWhList>> getWhListByParentId(@RequestParam Long parentId) {
        List<LxWhList> result = whListService.getWhListByParentId(parentId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询仓库
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxWhList> getWhDetail(@RequestParam Long id) {
        LxWhList wh = whListService.getWhById(id);
        if (wh != null) {
            return Result.success(wh);
        }
        return Result.error("仓库不存在");
    }
    
    /**
     * 根据仓库代码查询仓库
     */
    @RequiresPermission("view")
    @GetMapping("/by-code")
    public Result<LxWhList> getWhByCode(@RequestParam String whCode) {
        LxWhList wh = whListService.getWhByCode(whCode);
        if (wh != null) {
            return Result.success(wh);
        }
        return Result.error("仓库不存在");
    }
    
    /**
     * 创建仓库
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxWhList> createWh(@RequestBody LxWhList whList) {
        try {
            // 检查仓库代码是否已存在
            LxWhList existing = whListService.getWhByCode(whList.getWhCode());
            if (existing != null) {
                return Result.error("仓库代码已存在");
            }
            
            LxWhList result = whListService.createWh(whList);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建仓库失败", e);
            return Result.error("创建仓库失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新仓库
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateWh(@RequestBody LxWhList whList) {
        try {
            boolean success = whListService.updateWh(whList);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新仓库失败", e);
            return Result.error("更新仓库失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除仓库
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteWh(@RequestParam Long id) {
        try {
            boolean success = whListService.deleteWh(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除仓库失败", e);
            return Result.error("删除仓库失败：" + e.getMessage());
        }
    }
}

